package cst.neu.yuejian_31.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.activity.PostDetailActivity;
import cst.neu.yuejian_31.bean.PostBean;
import cst.neu.yuejian_31.Common.Constant;

/**
 * Created by xxe on 2017/10/12.
 */

public class PostListAdapter extends RecyclerView.Adapter  {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<PostBean>list;

    public PostListAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<PostBean>list){
        this.list=list;
        notifyDataSetChanged();
    }



    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView userPhoto,postPic;
        private TextView userName,postintro;

        public ViewHolder(View itemView) {
            super(itemView);
            userPhoto= (ImageView) itemView.findViewById(R.id.imageView_userphoto);
            userName= (TextView) itemView.findViewById(R.id.textView_username);
            postintro= (TextView) itemView.findViewById(R.id.textView_postintro);
            postPic= (ImageView) itemView.findViewById(R.id.imageView_postpic);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item_post,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final PostBean post =list.get(position);
        if (post==null)
            return;
        ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.userName.setText(post.getUsername());
        viewHolder.postintro.setText(post.getPost_intro());
        Picasso.with(context).load(Constant.POST_URL+post.getPost_pic())
                .placeholder(R.drawable.post)
                .into(viewHolder.postPic);
        Picasso.with(context).load(Constant.USER_URL+post.getUser_pic())
                .placeholder(R.drawable.user)
                .into(viewHolder.userPhoto);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, PostDetailActivity.class);
                intent.putExtra("post_id",post.getPost_id());
                intent.putExtra("post_intro",post.getPost_intro());
                intent.putExtra("post_pic",post.getPost_pic());
                intent.putExtra("post_time",post.getPost_time());
                intent.putExtra("user_name",post.getUsername());
                intent.putExtra("user_photo",post.getUser_pic());
                intent.putExtra("user_id",post.getUser_id());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if(list==null)return 0;
        else
            return list.size();
    }
}
